package dao;

import common.User;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class GetUser {
    public User getUser(User user1) {
        Connection conn = null;
        ResultSet res = null;
        User user2 = new User();
        try {
//            boolean f = false;
            Properties info=new Properties();
            info.load(new FileInputStream("src\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
            String sql = "select * from users where userid = ?";
            String sql1 = "update users set state = '" + user1.getState() + "' where userid = '" + user1.getId() + "'";
            PreparedStatement presta = conn.prepareStatement(sql);
            presta.setString(1, user1.getId());
            res = presta.executeQuery();
            if (res.next()) {
                user2.setId(res.getString("userid"));
                user2.setBirth(res.getString("birth"));
                user2.setSign(res.getString("sign"));
                user2.setHeadimage(res.getString("headimage"));
                user2.setEmail(res.getString("email"));
                user2.setPsw(res.getString("password"));
                user2.setAddress(res.getString("address"));
                user2.setGender(res.getString("gender"));
                user2.setName(res.getString("username"));
                user2.setState(1);
                PreparedStatement preparedStatement = conn.prepareStatement(sql1);
                System.out.println(user2);
                preparedStatement.executeUpdate();
                return user2;
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return user2;
    }
}
